
#include "file_photo.h"

#if 0
#define PHOTO_SEARCH_PATH  	  ("/home/waller")
#elif 1
#define PHOTO_SEARCH_PATH  	  ("/home/Photo")
#elif 1
#define PHOTO_SEARCH_PATH  	  ("/media/root")
#elif 0
#define PHOTO_SEARCH_PATH  	  ("/home/test")

#endif

static  pthread_t  photo_list_thread;

static  FileListHandle photo_file_list = NULL;

static  unsigned int  photo_total_num     = 0;
static  unsigned int  photo_search_flag   = 0;


unsigned int file_photo_get_total(void)
{
	return photo_total_num;
}


int file_photo_get_fname(char * filename,int index)
{
	int ret = -1;
	
	FileInfo    fileinfo;
	pFileList   filelist ;

	if(!filename)
		return -1;

	if(!photo_file_list)
		return -1;

	if(!photo_total_num)
		return -1;

	ret = file_list_get_file(photo_file_list,index,&fileinfo);
	if(ret != 0){
		return -1;
	}

	file_get_fullname(filename,fileinfo.path,fileinfo.name);

	//log_i("i:t:n=%04d:%04d:%s\n",index,photo_total_num,filename);

	return 0;
}


void* file_photo_list_task(void *arg)
{
	pFileList	filelist;

	while(1)
	{
		if(!photo_search_flag)
		{
			photo_search_flag = 1;
			
			file_list_read_all_file(photo_file_list,PHOTO_SEARCH_PATH,0);

			filelist = (pFileList)(photo_file_list);

			log_i("photo num = %d\n",filelist->filenum);

			photo_total_num = filelist->filenum;

			srand(photo_total_num); 
		}
		else
		{
			usleep(1000);
		}
	}
}

int file_photo_list_init(void)
{
	int ret  = 0;

	photo_file_list = file_list_create(IMAGE_FILE_LIST);
	if(!photo_file_list)
		return -1;

	ret=pthread_create(&photo_list_thread, NULL, file_photo_list_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
		return -1;
	}

	return 0;
}


int file_photo_list_deinit(void)
{
	if(photo_file_list){
		file_list_delete(photo_file_list);
		photo_file_list = NULL;
	}


	return 0;
}



